<?php


doService();
function doService(){
	$action=$_GET['action'];
	print_r($action);
	if($action=='upload'){
		upload();
	}else if($action=='getFileList'){
		getFileList();
	}else{
		echo 'action not exist!';
	}
	
}
function upload(){
	//设置编码为UTF-8，以避免中文乱码
	header('Content-Type:text/html;charset=utf-8');
	//获取多个文件的信息，注意：这里的键名不包含[]
	$fileArray = $_FILES['files'];
	//print_r($fileArray);
	//保存上传文件的目录
	$upload_dir =dirname(dirname(__FILE__)).'\\upload_file'; 
	print_r($upload_dir);
	foreach ( $fileArray['error'] as $key => $error) {
		//PHP常量UPLOAD_ERR_OK=0，表示上传没有出错
	    if ( $error == UPLOAD_ERR_OK ) { 
	        $temp_name = $fileArray['tmp_name'][$key];
	        $file_name = $fileArray['name'][$key];
			//将字符编码从UTF-8转为GBK
			$file_name_encode = iconv('UTF-8', 'GBK', $file_name);
			print_r($file_name);
	        move_uploaded_file($temp_name, $upload_dir.'\\'.$file_name_encode);
	        echo '上传[文件'.$file_name.']成功!<br/>';
	    }else {
	        echo '上传[文件'.$file_name.']失败!<br/>';
	    }
	}
}

function getFileList(){
	include '../util/UploadFileUtils.php';
	$upload_dir =dirname(dirname(__FILE__)).'\\upload_file'; 
	$fileList=UploadFileUtils::getFileList($upload_dir);
	$fileListJson=json_encode($fileList);
	echo $fileListJson;
}
?>